<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/**
<span class='line'>  2</span>  * Returns a {@link pv.Nest} operator for the specified array. This is a
<span class='line'>  3</span>  * convenience factory method, equivalent to &lt;tt>new pv.Nest(array)&lt;/tt>.
<span class='line'>  4</span>  *
<span class='line'>  5</span>  * @see pv.Nest
<span class='line'>  6</span>  * @param {array} array an array of elements to nest.
<span class='line'>  7</span>  * @returns {pv.Nest} a nest operator for the specified array.
<span class='line'>  8</span>  */</span><span class="WHIT">
<span class='line'>  9</span> </span><span class="NAME">pv.nest</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">array</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 10</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">new</span><span class="WHIT"> </span><span class="NAME">pv.Nest</span><span class="PUNC">(</span><span class="NAME">array</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 11</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 12</span> 
<span class='line'> 13</span> </span><span class="COMM">/**
<span class='line'> 14</span>  * Constructs a nest operator for the specified array. This constructor should
<span class='line'> 15</span>  * not be invoked directly; use {@link pv.nest} instead.
<span class='line'> 16</span>  *
<span class='line'> 17</span>  * @class Represents a {@link Nest} operator for the specified array. Nesting
<span class='line'> 18</span>  * allows elements in an array to be grouped into a hierarchical tree
<span class='line'> 19</span>  * structure. The levels in the tree are specified by &lt;i>key&lt;/i> functions. The
<span class='line'> 20</span>  * leaf nodes of the tree can be sorted by value, while the internal nodes can
<span class='line'> 21</span>  * be sorted by key. Finally, the tree can be returned either has a
<span class='line'> 22</span>  * multidimensional array via {@link #entries}, or as a hierarchical map via
<span class='line'> 23</span>  * {@link #map}. The {@link #rollup} routine similarly returns a map, collapsing
<span class='line'> 24</span>  * the elements in each leaf node using a summary function.
<span class='line'> 25</span>  *
<span class='line'> 26</span>  * &lt;p>For example, consider the following tabular data structure of Barley
<span class='line'> 27</span>  * yields, from various sites in Minnesota during 1931-2:
<span class='line'> 28</span>  *
<span class='line'> 29</span>  * &lt;pre>{ yield: 27.00, variety: "Manchuria", year: 1931, site: "University Farm" },
<span class='line'> 30</span>  * { yield: 48.87, variety: "Manchuria", year: 1931, site: "Waseca" },
<span class='line'> 31</span>  * { yield: 27.43, variety: "Manchuria", year: 1931, site: "Morris" }, ...&lt;/pre>
<span class='line'> 32</span>  *
<span class='line'> 33</span>  * To facilitate visualization, it may be useful to nest the elements first by
<span class='line'> 34</span>  * year, and then by variety, as follows:
<span class='line'> 35</span>  *
<span class='line'> 36</span>  * &lt;pre>var nest = pv.nest(yields)
<span class='line'> 37</span>  *     .key(function(d) d.year)
<span class='line'> 38</span>  *     .key(function(d) d.variety)
<span class='line'> 39</span>  *     .entries();&lt;/pre>
<span class='line'> 40</span>  *
<span class='line'> 41</span>  * This returns a nested array. Each element of the outer array is a key-values
<span class='line'> 42</span>  * pair, listing the values for each distinct key:
<span class='line'> 43</span>  *
<span class='line'> 44</span>  * &lt;pre>{ key: 1931, values: [
<span class='line'> 45</span>  *   { key: "Manchuria", values: [
<span class='line'> 46</span>  *       { yield: 27.00, variety: "Manchuria", year: 1931, site: "University Farm" },
<span class='line'> 47</span>  *       { yield: 48.87, variety: "Manchuria", year: 1931, site: "Waseca" },
<span class='line'> 48</span>  *       { yield: 27.43, variety: "Manchuria", year: 1931, site: "Morris" },
<span class='line'> 49</span>  *       ...
<span class='line'> 50</span>  *     ] },
<span class='line'> 51</span>  *   { key: "Glabron", values: [
<span class='line'> 52</span>  *       { yield: 43.07, variety: "Glabron", year: 1931, site: "University Farm" },
<span class='line'> 53</span>  *       { yield: 55.20, variety: "Glabron", year: 1931, site: "Waseca" },
<span class='line'> 54</span>  *       ...
<span class='line'> 55</span>  *     ] },
<span class='line'> 56</span>  *   ] },
<span class='line'> 57</span>  * { key: 1932, values: ... }&lt;/pre>
<span class='line'> 58</span>  *
<span class='line'> 59</span>  * Further details, including sorting and rollup, is provided below on the
<span class='line'> 60</span>  * corresponding methods.
<span class='line'> 61</span>  *
<span class='line'> 62</span>  * @param {array} array an array of elements to nest.
<span class='line'> 63</span>  */</span><span class="WHIT">
<span class='line'> 64</span> </span><span class="NAME">pv.Nest</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">array</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 65</span> </span><span class="WHIT">  </span><span class="NAME">this.array</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">array</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 66</span> </span><span class="WHIT">  </span><span class="NAME">this.keys</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 67</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 68</span> 
<span class='line'> 69</span> </span><span class="COMM">/**
<span class='line'> 70</span>  * Nests using the specified key function. Multiple keys may be added to the
<span class='line'> 71</span>  * nest; the array elements will be nested in the order keys are specified.
<span class='line'> 72</span>  *
<span class='line'> 73</span>  * @param {function} key a key function; must return a string or suitable map
<span class='line'> 74</span>  * key.
<span class='line'> 75</span>  * @return {pv.Nest} this.
<span class='line'> 76</span>  */</span><span class="WHIT">
<span class='line'> 77</span> </span><span class="NAME">pv.Nest.prototype.key</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">key</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 78</span> </span><span class="WHIT">  </span><span class="NAME">this.keys.push</span><span class="PUNC">(</span><span class="NAME">key</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 79</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">this</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 80</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 81</span> 
<span class='line'> 82</span> </span><span class="COMM">/**
<span class='line'> 83</span>  * Sorts the previously-added keys. The natural sort order is used by default
<span class='line'> 84</span>  * (see {@link pv.naturalOrder}); if an alternative order is desired,
<span class='line'> 85</span>  * &lt;tt>order&lt;/tt> should be a comparator function. If this method is not called
<span class='line'> 86</span>  * (i.e., keys are &lt;i>unsorted&lt;/i>), keys will appear in the order they appear
<span class='line'> 87</span>  * in the underlying elements array. For example,
<span class='line'> 88</span>  *
<span class='line'> 89</span>  * &lt;pre>pv.nest(yields)
<span class='line'> 90</span>  *     .key(function(d) d.year)
<span class='line'> 91</span>  *     .key(function(d) d.variety)
<span class='line'> 92</span>  *     .sortKeys()
<span class='line'> 93</span>  *     .entries()&lt;/pre>
<span class='line'> 94</span>  *
<span class='line'> 95</span>  * groups yield data by year, then variety, and sorts the variety groups
<span class='line'> 96</span>  * lexicographically (since the variety attribute is a string).
<span class='line'> 97</span>  *
<span class='line'> 98</span>  * &lt;p>Key sort order is only used in conjunction with {@link #entries}, which
<span class='line'> 99</span>  * returns an array of key-values pairs. If the nest is used to construct a
<span class='line'>100</span>  * {@link #map} instead, keys are unsorted.
<span class='line'>101</span>  *
<span class='line'>102</span>  * @param {function} [order] an optional comparator function.
<span class='line'>103</span>  * @returns {pv.Nest} this.
<span class='line'>104</span>  */</span><span class="WHIT">
<span class='line'>105</span> </span><span class="NAME">pv.Nest.prototype.sortKeys</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">order</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>106</span> </span><span class="WHIT">  </span><span class="NAME">this.keys</span><span class="PUNC">[</span><span class="NAME">this.keys.length</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">order</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">order</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="NAME">pv.naturalOrder</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>107</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">this</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>108</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>109</span> 
<span class='line'>110</span> </span><span class="COMM">/**
<span class='line'>111</span>  * Sorts the leaf values. The natural sort order is used by default (see
<span class='line'>112</span>  * {@link pv.naturalOrder}); if an alternative order is desired, &lt;tt>order&lt;/tt>
<span class='line'>113</span>  * should be a comparator function. If this method is not called (i.e., values
<span class='line'>114</span>  * are &lt;i>unsorted&lt;/i>), values will appear in the order they appear in the
<span class='line'>115</span>  * underlying elements array. For example,
<span class='line'>116</span>  *
<span class='line'>117</span>  * &lt;pre>pv.nest(yields)
<span class='line'>118</span>  *     .key(function(d) d.year)
<span class='line'>119</span>  *     .key(function(d) d.variety)
<span class='line'>120</span>  *     .sortValues(function(a, b) a.yield - b.yield)
<span class='line'>121</span>  *     .entries()&lt;/pre>
<span class='line'>122</span>  *
<span class='line'>123</span>  * groups yield data by year, then variety, and sorts the values for each
<span class='line'>124</span>  * variety group by yield.
<span class='line'>125</span>  *
<span class='line'>126</span>  * &lt;p>Value sort order, unlike keys, applies to both {@link #entries} and
<span class='line'>127</span>  * {@link #map}. It has no effect on {@link #rollup}.
<span class='line'>128</span>  *
<span class='line'>129</span>  * @param {function} [order] an optional comparator function.
<span class='line'>130</span>  * @return {pv.Nest} this.
<span class='line'>131</span>  */</span><span class="WHIT">
<span class='line'>132</span> </span><span class="NAME">pv.Nest.prototype.sortValues</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">order</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>133</span> </span><span class="WHIT">  </span><span class="NAME">this.order</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">order</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="NAME">pv.naturalOrder</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>134</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">this</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>135</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>136</span> 
<span class='line'>137</span> </span><span class="COMM">/**
<span class='line'>138</span>  * Returns a hierarchical map of values. Each key adds one level to the
<span class='line'>139</span>  * hierarchy. With only a single key, the returned map will have a key for each
<span class='line'>140</span>  * distinct value of the key function; the correspond value with be an array of
<span class='line'>141</span>  * elements with that key value. If a second key is added, this will be a nested
<span class='line'>142</span>  * map. For example:
<span class='line'>143</span>  *
<span class='line'>144</span>  * &lt;pre>pv.nest(yields)
<span class='line'>145</span>  *     .key(function(d) d.variety)
<span class='line'>146</span>  *     .key(function(d) d.site)
<span class='line'>147</span>  *     .map()&lt;/pre>
<span class='line'>148</span>  *
<span class='line'>149</span>  * returns a map &lt;tt>m&lt;/tt> such that &lt;tt>m[variety][site]&lt;/tt> is an array, a subset of
<span class='line'>150</span>  * &lt;tt>yields&lt;/tt>, with each element having the given variety and site.
<span class='line'>151</span>  *
<span class='line'>152</span>  * @returns a hierarchical map of values.
<span class='line'>153</span>  */</span><span class="WHIT">
<span class='line'>154</span> </span><span class="NAME">pv.Nest.prototype.map</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>155</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">map</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">values</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>156</span> 
<span class='line'>157</span> </span><span class="WHIT">  </span><span class="COMM">/* Build the map. */</span><span class="WHIT">
<span class='line'>158</span> </span><span class="WHIT">  </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">this.array.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">j</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>159</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">x</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.array</span><span class="PUNC">[</span><span class="NAME">j</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>160</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">m</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>161</span> </span><span class="WHIT">    </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">this.keys.length</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NUMB">1</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>162</span> </span><span class="WHIT">      </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">k</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.keys</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>163</span> </span><span class="WHIT">      </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">m</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">m</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>164</span> </span><span class="WHIT">      </span><span class="NAME">m</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">m</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>165</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>166</span> </span><span class="WHIT">    </span><span class="NAME">k</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.keys</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>167</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">m</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>168</span> </span><span class="WHIT">      </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">a</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>169</span> </span><span class="WHIT">      </span><span class="NAME">values.push</span><span class="PUNC">(</span><span class="NAME">a</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>170</span> </span><span class="WHIT">      </span><span class="NAME">m</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">a</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>171</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>172</span> </span><span class="WHIT">    </span><span class="NAME">m</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">push</span><span class="PUNC">(</span><span class="NAME">x</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>173</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>174</span> 
<span class='line'>175</span> </span><span class="WHIT">  </span><span class="COMM">/* Sort each leaf array. */</span><span class="WHIT">
<span class='line'>176</span> </span><span class="WHIT">  </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">this.order</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>177</span> </span><span class="WHIT">    </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">values.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>178</span> </span><span class="WHIT">      </span><span class="NAME">values</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">sort</span><span class="PUNC">(</span><span class="NAME">this.order</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>179</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>180</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>181</span> 
<span class='line'>182</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>183</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>184</span> 
<span class='line'>185</span> </span><span class="COMM">/**
<span class='line'>186</span>  * Returns a hierarchical nested array. This method is similar to
<span class='line'>187</span>  * {@link pv.entries}, but works recursively on the entire hierarchy. Rather
<span class='line'>188</span>  * than returning a map like {@link #map}, this method returns a nested
<span class='line'>189</span>  * array. Each element of the array has a &lt;tt>key&lt;/tt> and &lt;tt>values&lt;/tt>
<span class='line'>190</span>  * field. For leaf nodes, the &lt;tt>values&lt;/tt> array will be a subset of the
<span class='line'>191</span>  * underlying elements array; for non-leaf nodes, the &lt;tt>values&lt;/tt> array will
<span class='line'>192</span>  * contain more key-values pairs.
<span class='line'>193</span>  *
<span class='line'>194</span>  * &lt;p>For an example usage, see the {@link Nest} constructor.
<span class='line'>195</span>  *
<span class='line'>196</span>  * @returns a hierarchical nested array.
<span class='line'>197</span>  */</span><span class="WHIT">
<span class='line'>198</span> </span><span class="NAME">pv.Nest.prototype.entries</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>199</span> 
<span class='line'>200</span> </span><span class="WHIT">  </span><span class="COMM">/** Recursively extracts the entries for the given map. */</span><span class="WHIT">
<span class='line'>201</span> </span><span class="WHIT">  </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">entries</span><span class="PUNC">(</span><span class="NAME">map</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>202</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">array</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="PUNC">[</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>203</span> </span><span class="WHIT">    </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">k</span><span class="WHIT"> </span><span class="KEYW">in</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>204</span> </span><span class="WHIT">      </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">v</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">[</span><span class="NAME">k</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>205</span> </span><span class="WHIT">      </span><span class="NAME">array.push</span><span class="PUNC">(</span><span class="PUNC">{</span><span class="WHIT"> </span><span class="NAME">key</span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">k</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">values</span><span class="PUNC">:</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">v</span><span class="WHIT"> </span><span class="KEYW">instanceof</span><span class="WHIT"> </span><span class="NAME">Array</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">?</span><span class="WHIT"> </span><span class="NAME">v</span><span class="WHIT"> </span><span class="PUNC">:</span><span class="WHIT"> </span><span class="NAME">entries</span><span class="PUNC">(</span><span class="NAME">v</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>206</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>207</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">array</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>208</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>209</span> 
<span class='line'>210</span> </span><span class="WHIT">  </span><span class="COMM">/** Recursively sorts the values for the given key-values array. */</span><span class="WHIT">
<span class='line'>211</span> </span><span class="WHIT">  </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">sort</span><span class="PUNC">(</span><span class="NAME">array</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>212</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">o</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">this.keys</span><span class="PUNC">[</span><span class="NAME">i</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">order</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>213</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">o</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="NAME">array.sort</span><span class="PUNC">(</span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">a</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">b</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT"> </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">o</span><span class="PUNC">(</span><span class="NAME">a.key</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">b.key</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="PUNC">}</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>214</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">++</span><span class="NAME">i</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">this.keys.length</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>215</span> </span><span class="WHIT">      </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">j</span><span class="WHIT"> </span><span class="PUNC">&lt;</span><span class="WHIT"> </span><span class="NAME">array.length</span><span class="PUNC">;</span><span class="WHIT"> </span><span class="NAME">j</span><span class="PUNC">++</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>216</span> </span><span class="WHIT">        </span><span class="NAME">sort.call</span><span class="PUNC">(</span><span class="KEYW">this</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">array</span><span class="PUNC">[</span><span class="NAME">j</span><span class="PUNC">]</span><span class="PUNC">.</span><span class="NAME">values</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">i</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>217</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>218</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>219</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">array</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>220</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>221</span> 
<span class='line'>222</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">sort.call</span><span class="PUNC">(</span><span class="KEYW">this</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">entries</span><span class="PUNC">(</span><span class="NAME">this.map</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>223</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>224</span> 
<span class='line'>225</span> </span><span class="COMM">/**
<span class='line'>226</span>  * Returns a rollup map. The behavior of this method is the same as
<span class='line'>227</span>  * {@link #map}, except that the leaf values are replaced with the return value
<span class='line'>228</span>  * of the specified rollup function &lt;tt>f&lt;/tt>. For example,
<span class='line'>229</span>  *
<span class='line'>230</span>  * &lt;pre>pv.nest(yields)
<span class='line'>231</span>  *      .key(function(d) d.site)
<span class='line'>232</span>  *      .rollup(function(v) pv.median(v, function(d) d.yield))&lt;/pre>
<span class='line'>233</span>  *
<span class='line'>234</span>  * first groups yield data by site, and then returns a map from site to median
<span class='line'>235</span>  * yield for the given site.
<span class='line'>236</span>  *
<span class='line'>237</span>  * @see #map
<span class='line'>238</span>  * @param {function} f a rollup function.
<span class='line'>239</span>  * @returns a hierarchical map, with the leaf values computed by &lt;tt>f&lt;/tt>.
<span class='line'>240</span>  */</span><span class="WHIT">
<span class='line'>241</span> </span><span class="NAME">pv.Nest.prototype.rollup</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="PUNC">(</span><span class="NAME">f</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>242</span> 
<span class='line'>243</span> </span><span class="WHIT">  </span><span class="COMM">/** Recursively descends to the leaf nodes (arrays) and does rollup. */</span><span class="WHIT">
<span class='line'>244</span> </span><span class="WHIT">  </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="NAME">rollup</span><span class="PUNC">(</span><span class="NAME">map</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>245</span> </span><span class="WHIT">    </span><span class="KEYW">for</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">key</span><span class="WHIT"> </span><span class="KEYW">in</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>246</span> </span><span class="WHIT">      </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">value</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">[</span><span class="NAME">key</span><span class="PUNC">]</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>247</span> </span><span class="WHIT">      </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">value</span><span class="WHIT"> </span><span class="KEYW">instanceof</span><span class="WHIT"> </span><span class="NAME">Array</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>248</span> </span><span class="WHIT">        </span><span class="NAME">map</span><span class="PUNC">[</span><span class="NAME">key</span><span class="PUNC">]</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">f</span><span class="PUNC">(</span><span class="NAME">value</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>249</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>250</span> </span><span class="WHIT">        </span><span class="NAME">rollup</span><span class="PUNC">(</span><span class="NAME">value</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>251</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>252</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>253</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">map</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>254</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>255</span> 
<span class='line'>256</span> </span><span class="WHIT">  </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="NAME">rollup</span><span class="PUNC">(</span><span class="NAME">this.map</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>257</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>258</span> </span></pre></body></html>